package service

import (
	"fmt"
	"github.com/gin-gonic/gin"
)

type GroupService struct {
}

//因为客户端没有做上线处理   发布出去
func (GroupService) AddGroup(c *gin.Context) {
	//TODO  从请求参数中获取构造信息   初始化群设定
	id := c.PostForm("id")

	fmt.Printf("添加id为 %s 的群聊\r", id)

	group := &Group{
		ID:        id,
		Members:   make(map[string]bool),
		Clients:   make(map[string]*Client),
		Broadcast: make(chan *Broadcast),
	}
	go group.GroupInit() // 开始监听
	fmt.Println("卡在这里了？")
	Manager.Groups = append(Manager.Groups, group)

	c.JSON(200, gin.H{
		"message": "添加好了 重启就没了",
	})

}

// 为群聊添加用户
//  10001   4 5  6
func (GroupService) AddUser(c *gin.Context) {

	userId := c.PostForm("userId")
	groupId := c.PostForm("groupId")
	fmt.Printf("往%s 添加用户 %s \r", groupId, userId)
	for _, group := range Manager.Groups {
		if group.ID == groupId {
			group.Members[userId] = true
			break
		}
	}

	c.JSON(200, gin.H{
		"message": "添加完成了  但是断电就没了呀",
	})

}
